package com.dalei.vdserver.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.dalei.vdserver.model.Settings;

public class SettingsDaoImpl extends JdbcDaoSupport implements SettingsDao {

	@Override
	public Settings getEnabledSettingsGroup() {
		// TODO Auto-generated method stub
		final String sql = "SELECT * FROM vds_settings WHERE _enabled = 1";
		Settings settings = null;
		try {
			settings = (Settings) getJdbcTemplate().queryForObject(
					sql, new RowMapper() {

						@Override
						public Object mapRow(ResultSet rs, int rowNum)
								throws SQLException {
							// TODO Auto-generated method stub
							Settings s = new Settings();
							s.setId(rs.getInt("_id"));
							s.setGroupName(rs.getString("_groupName"));
							s.setSessionTimeout(rs.getLong("_sessionTimeout"));
							s.setEnabled(rs.getBoolean("_enabled"));
							return s;
						}
					});
		} catch (IncorrectResultSizeDataAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		} catch (DataAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		return settings;
	}
}
